package com.bangdream.michelia.view.fragment.main.task;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.NoScrollViewPager;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;

/**
 * @date on 2018/6/18 15:44
 * @email 909732184@qq.com
 * 首页-任务
 */
public class MainTask extends BaseFragment implements ViewPager.OnPageChangeListener,View.OnClickListener{
    private View mView;
    private View statusBarLayout;
    private TextView[] btnTvs = new TextView[4];
    private NoScrollViewPager mViewPage;
    private FragmentPagerAdapter mAdapter;
    //选项卡被选中和未选中时的字体颜色
    private int onTextColor,offTextColor;
    //选项卡被选中和未选中时的背景资源
    private Drawable onBgColor,offBgColor;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.main_item_task,container,false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    private void initListener() {
        mViewPage.addOnPageChangeListener(this);
        for(TextView textView:btnTvs){
            textView.setOnClickListener(this);
        }
    }

    private void initView() {
        statusBarLayout = mView.findViewById(R.id.statusBarLayout);
        ViewGroup.LayoutParams layoutParams = statusBarLayout.getLayoutParams();
        layoutParams.height = MainActivity.statusBarHeight;
        statusBarLayout.setLayoutParams(layoutParams);
        onTextColor = ContextCompat.getColor(getActivity(),R.color.common_red);
        offTextColor = ContextCompat.getColor(getActivity(),R.color.white);
        onBgColor = ContextCompat.getDrawable(getActivity(),R.drawable.cutting_white_bg);
        offBgColor = null;
        btnTvs[0] = mView.findViewById(R.id.btnTv1);
        btnTvs[1] = mView.findViewById(R.id.btnTv2);
        btnTvs[2] = mView.findViewById(R.id.btnTv3);
        btnTvs[3] = mView.findViewById(R.id.btnTv4);
//        btnTvs[4] = mView.findViewById(R.id.btnTv5);
        mViewPage = mView.findViewById(R.id.mViewPage);
        mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position){
                    case 0:
                        return new TaskCurriculumList();
                    case 1:
                        return new TaskTrainPlanList();
                    case 2:
                        final Bundle bundle = new Bundle();
                        bundle.putString("type","2");
                        TaskExaminationList fragment = new TaskExaminationList();
                        fragment.setArguments(bundle);
                        return fragment;
                    case 3:
                        final Bundle bundleQuest= new Bundle();
                        bundleQuest.putString("type","2");
                        TaskQuestQueryList fragmentQuest = new TaskQuestQueryList();
                        fragmentQuest.setArguments(bundleQuest);
                        return fragmentQuest;
//                    case 4:
//                        return new TaskOrderList();
                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                return btnTvs.length;
            }
        };
        mViewPage.setAdapter(mAdapter);
        //总页数和10比较取小 设置缓存页数
        mViewPage.setOffscreenPageLimit(Math.min(mAdapter.getCount(),10));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for(int i = 0;i<btnTvs.length;i++){
            if(i==position){
                btnTvs[i].setTextColor(onTextColor);
                btnTvs[i].setBackground(onBgColor);
            }
            else {
                btnTvs[i].setTextColor(offTextColor);
                btnTvs[i].setBackground(offBgColor);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnTv1:
                mViewPage.setCurrentItem(0);
                break;
            case R.id.btnTv2:
                mViewPage.setCurrentItem(1);
                break;
            case R.id.btnTv3:
                mViewPage.setCurrentItem(2);
                break;
            case R.id.btnTv4:
                mViewPage.setCurrentItem(3);
                break;
//            case R.id.btnTv5:
//                mViewPage.setCurrentItem(4);
//                break;
            default:break;
        }
    }
}
